package com.test04_数组;

import org.junit.Test;

/**
 * 方法可变参数
 */
public class YootkDemo_05 {

    @Test
    public void test1() {
        System.out.println("零个数字数据累加：" + MathUtil.sum());
        System.out.println("三个数字数据累加：" + MathUtil.sum(1, 3, 5));
        System.out.println("五个数字数据累加：" + MathUtil.sum(1, 3, 5, 7, 9));
    }

    @Test
    public void test2() {
        int[] array = {1, 3, 5};
        System.out.println("数据加法操作：" + MathUtil.calc("+", array));
        System.out.println("数据减法操作：" + MathUtil.calc("-", array));
        System.out.println("数据乘法操作：" + MathUtil.calc("*", array));
        System.out.println("数据除法操作：" + MathUtil.calc("/", array));
    }
}

/**
 * 数学工具类
 */
class MathUtil {

    // 数组求和
    public static int sum(int... args) {
        int result = 0;
        for (int x : args) {
            result += x;
        }
        return result;
    }

    // 数组四维运算
    public static int calc(String oper, int... args) {
        int result = 0;
        switch (oper) {
            case "+": {
                for (int num : args) {
                    result += num;
                }
                break;
            }
            case "-": {
                for (int num : args) {
                    result -= num;
                }
                break;
            }
            case "*": {
                result = 1;
                for (int num : args) {
                    result *= num;
                }
                break;
            }
            case "/": {
                result = 1;
                for (int num : args) {
                    result /= num;
                }
                break;
            }
        }
        return result;
    }
}
